<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="审核状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择头像审核状态"
                class="ele-fluid">
                <el-option label="待审核" value="0"/>
                <el-option label="已拒绝" value="-1"/>
                <el-option label="已通过" value="1"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">

        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="editStatus(row,1)"
            v-if="permission.includes('sys:examine:status') && row.status ==0">通过
          </el-link>
          <el-link
            type="danger"
            :underline="false"
            icon="el-icon-delete"
            @click="openEdit(row,1)"
            v-if="permission.includes('sys:examine:status') && row.status ==0">拒绝
          </el-link>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-tag v-if="row.status == 1" type="success" size="small">已通过</el-tag>
          <el-tag v-if="row.status == -1" type="warning" size="small">已拒绝</el-tag>
          <el-tag v-if="row.status ==  0" size="small">待审核</el-tag>
        </template>
        <!-- 头像 -->
        <template slot="new_value" slot-scope="{row}">
          <el-image style="width: 50px; height: 50px"
                    :src="row.new_value" :preview-src-list="[row.new_value]"/>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit
      :data="current"
      :visible.sync="showEdit"
      @done="canalStatus"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import edit from './edit';

export default {
  name: 'SystemLink',
  components: {edit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/BoxAvatarExamine/avatarList',
      // 表格列配置
      columns: [
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          columnKey: 'new_value',
          label: '头像icon',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'new_value'
        },
        {
          prop: 'status',
          label: '审核状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
        {
          prop: 'reason',
          label: '拒绝原因',
          align: 'center',
          showOverflowTooltip: true,
          width: 200
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '审核时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示原因 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 拒绝 */
    canalStatus(data) {
      this.editStatus(data, -1, data.reason)
      console.log(data)
    },
    /* 更改状态 */
    editStatus(row, status, reason = '') {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": status,
        'reason': reason,
      })
      this.$http.post('/BoxAvatarExamine/avatarStatus', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
